package controller;

import dao.DaoException;
import dao.manager.ManagerDao;
import dao.manager.ManagerDaoImpl;
import dao.reader.ReaderDao;
import dao.reader.ReaderDaoImpl;
import model.Manager;
import model.Reader;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "LoginServlet", urlPatterns = {"/login", "/register"})
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        if (uri.endsWith("register")) {
            try {
                Register(request, response);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        } else if (uri.endsWith("login")) {
            try {
                Login(request, response);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        }
    }

    private void Register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, DaoException {
        //int id=Integer.parseInt(request.getParameter("id"));

        String username = new String(request.getParameter("username").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String password = request.getParameter("password");
        String telephone = request.getParameter("telephone");
        Reader reader = new Reader(username, password, telephone);
        ReaderDao dao = new ReaderDaoImpl();
        if (dao.addReader(reader)) {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/login.jsp");//注册页面
            rd.forward(request, response);
        } else {
            System.out.println("注册失败");
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/login.jsp");//注册页面
            rd.forward(request, response);
        }


    }

    private void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, DaoException {
        String username = new String(request.getParameter("username").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        System.out.println(username);
        String password = request.getParameter("password");
        String identity = request.getParameter("identity");
        HttpSession session = request.getSession();
        //这里判断其为用户
        if (identity.equals("user")) {

            ReaderDao dao = new ReaderDaoImpl();
            Reader reader = dao.readerLogin(username, password);

            if (reader != null) {//登录成功
                session.setAttribute("reader", reader);
                response.sendRedirect("borrowBooksDetail.jsp");

            } else {
                request.setAttribute("errs", "用户名或密码不正确！");
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/login.jsp");
                rd.forward(request, response);
            }
        } else {
            //这里是管理员。
            ManagerDao managerDao = new ManagerDaoImpl();
            Manager manager = managerDao.managerLogin(username, password);
            if (manager != null) {
                //这里表示登陆成功。
                session.setAttribute("manager", manager);
                response.sendRedirect("/manageBooks.jsp");
            } else {
                request.setAttribute("errs", "管理员名或密码不正确！");
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/login.jsp");
                rd.forward(request, response);
            }

        }

    }


}



